export default class UndoManager {
  constructor() {
    this.actionStack = []
    this.undoneStack = []
  }

  pushAction(action) {
    this.undoneStack = []
    this.actionStack.push(action)
    action.activate()
  }

  undoLastAction() {
    if (this.actionStack.length === 0) {
      return
    }

    const action = this.actionStack.pop()
    this.undoneStack.push(action)

    action.undo()
  }

  redoLastUndoneAction() {
    if (this.undoneStack.length === 0) {
      return
    }

    const action = this.undoneStack.pop()
    this.actionStack.push(action)
    action.activate()
  }

  get safeToPushAction() {
    // Is it safe to push a new action? That is, since pushing a new action
    // clears the undone actions stack, will any undone actions be lost?

    return this.undoStack.length === 0
  }
}
